package cc.ufinity.networkdiagnose.widget;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.view.R;

public class SnrTestWidgetProxy extends GeneralTestWidget {

    private Activity ctx;
    private Handler handlerChild;
    private final Handler handler;   
    private Task task;
    public static int testFlag = 0;  
    public static int taskFlag = 0;  
	private TelManager tm;
	
	private boolean TEL_SNR_SUCCESS = false;
	private boolean isFirst = true;
	private boolean isTimeOut = false;
 
    public SnrTestWidgetProxy(Activity context, Handler handl) {
        super(context);
        this.ctx = context;
        this.handlerChild = handl;
        inflate(context, R.layout.webauth_test_widget_proxy, this); 
        task = (Task)ctx.getIntent().getSerializableExtra(TAG_REFER_TASK);
        handler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MessageWhat.DO_NEXT_TEST:
                  //  showNextText();
                    break;  
                case MessageWhat.TEL_STATE_SUCCESS:
                	
                	if(!TEL_SNR_SUCCESS) {
                		TEL_SNR_SUCCESS = true;
                		
                		if(isTimeOut) {
                    		System.out.println(" 信噪比测试超时 ");
                    	}
                		
                		updateTel();
                	}
					break;
                }
            }
        }; 
		tm = new TelManager(ctx);
		tm.setHandler(handler);
		setTimeOut(10000);   //设置超时
    }
    
    private void updateTel() {
    	if(task.selNetType == ConnectivityManager.TYPE_MOBILE || task.selNetType == ConnectivityManager.TYPE_MOBILE_MMS)
        {
        	  task.snr = String.valueOf(tm.getEvdoSnr());
        	  System.out.println("TD网络 信噪比： " + tm.getEvdoSnr());
              //----------------------------------------------------------
        		if(isFirst) {
        			isFirst = false;
        			StringBuffer temp = new StringBuffer();
        			GlobalApp.result_SNR_test = temp;
        		}
        		GlobalApp.result_SNR_test.append("\nSNR: " + task.snr);
        	//----------------------------------------------------------
        } else {
        	  int level = task.scanResult==null?0:task.scanResult.level;
        	  task.snr = String.valueOf(level*(-1));
              //----------------------------------------------------------
        		if(isFirst) {
        			isFirst = false;
        			StringBuffer temp = new StringBuffer();
        			GlobalApp.result_SNR_test = temp;
        		}
        		GlobalApp.result_SNR_test.append("SSID: " + task.scanResult.SSID)
        				.append("\nMAC: " + task.scanResult.BSSID)
        				.append("\nSNR: " + task.snr);
        	//----------------------------------------------------------
		}
    	

System.out.println("task.snr => " + task.snr);
    	
       // mSendBroadcast(ctx); 
        handlerSendToTarget(handlerChild, MessageWhat.HANDLER_SNR);
        ctx.getIntent().putExtra(TAG_REFER_TASK, task); 

	} 
  
    private void setTimeOut(long timOut) {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
            	if(!isTimeOut) {
                	isTimeOut = true;
                }
            	Message msg = handler.obtainMessage();
            	msg.what = MessageWhat.TEL_STATE_SUCCESS;
            	msg.sendToTarget();
            }
        };
        Timer t = new Timer();
        t.schedule(timerTask, timOut); 
    } 
  
}
